<template>
    <el-dialog v-model="updateGoodsShow" title="修改商品"  width="30%" center>
        <el-form :model="goodsStore" :rules="rules" ref="dom" label-width="120px">
            <el-form-item label="商品条码" prop="code">
                <el-input v-model="goodsStore.code" style=" max-width: 250px;" disabled/>
            </el-form-item>
            <el-form-item label="商品名称">
                <el-input v-model="goodsStore.goodsName" style=" max-width: 250px;"/>
            </el-form-item>
            <el-form-item label="零售价" prop="price">
                <el-input v-model="goodsStore.price" style=" max-width: 100px;"/>
                <el-button style="margin-left: 5%" type="primary" plain @click="getSuggestedPrice">建议价</el-button>
            </el-form-item>
            <el-form-item label="成本价" >
                <el-input v-model="goodsStore.cost" style=" max-width: 100px;"/>
            </el-form-item>
            <el-form-item label="库存" >
                <el-input v-model="goodsStore.inventory" style=" max-width: 100px;"/>
            </el-form-item>
            <el-form-item label="自定义库存预警" >
                <el-input v-model="goodsStore.warn" style=" max-width: 100px;"/>
            </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="updateGoodsShow = false">取消</el-button>
        <el-button type="primary" @click="submit(dom)">
          确定
        </el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script setup>

import {onMounted, reactive, ref} from "vue";
import {store} from "@/store";
import {ElMessage} from "element-plus";
import request from "@/utils/request";

onMounted(()=>{
  setTimeout(()=>{
    goodsStore.value.storeId=store.getLocalCurrentStoreMain().storeId
  },1000)
})
const goodsStore=ref({
    code:"",
    goodsName:"",
    storeId:0,
    inventory:0,
    cost:0.0,
    price:0.0,
    warn:3
})
let updateGoodsShow=ref(false)
const dom = ref()
const emit = defineEmits(['confirmUpdate'])
const show = (data) => {
    updateGoodsShow.value=true
    request.get("/goods/get/goodsInfoByCode2/"+store.getLocalCurrentStoreMain().storeId+"/"+data.code).then(res=>{
        goodsStore.value=res.data
    })
}
const validateCode = (rule, value) => {
    if (value.length !== 13)
        return Promise.reject("请输入正确的13位条形码")
    else
        return Promise.resolve()

}
const validatePrice = (rule, value) => {
    if (value<=0)
        return Promise.reject("价格必须大于0")
    else
        return Promise.resolve()

}
const rules = reactive({
    code:[{validator: validateCode, trigger: 'change',required:true}],
    price:[{validator: validatePrice, trigger: 'change',required:true}]
})

const getSuggestedPrice = () => {
    if (goodsStore.value.code.length!==13){
        ElMessage({
            message: "请输入正确的商品条码",
            type: 'warning',
        })
    }else {
        request.get("/goods/get/suggestedPrice/"+goodsStore.value.code).then(res=>{
            if (res.data!==null)
                goodsStore.value.price=res.data
            else
                ElMessage({
                    message: "没有此商品的建议价",
                    type: 'warning',
                })
        })
    }
}
const submit = (dom) => {
    dom.validate((isTrue) => {
        if (isTrue) {
            goodsStore.value.storeId=store.getLocalCurrentStoreMain().storeId
            request.post("/goods/update/goodsStore", [goodsStore.value]).then(response => {
                if (response.code === 200) {
                    emit("confirmUpdate")
                    updateGoodsShow.value=false
                } else {
                    console.log(response)
                    ElMessage({
                        message: response.message,
                        type:'error'
                    })
                }
            })
        } else {
            console.log("格式有误")
        }
    })
}


defineExpose({
    show,
})
</script>

<style scoped>

</style>